/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package facades;

import entities.C;
import entities.interfaces.PedidoInterfaz;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

/**
 *
 * @author damfercer
 */
@Stateless
public class CFacade extends AbstractFacade<C> {
    @PersistenceContext(unitName = "CorporatePU")
    private EntityManager em;

    @Override
    protected EntityManager getEntityManager() {
        return em;
    }

    public CFacade() {
        super(C.class);
    }
    
    public List<C> filtrarC(String sql){
        StringBuilder sb=new StringBuilder();
        sb.append("SELECT DISTINCT c.* FROM c, producto WHERE producto.C=c.ID_C");
        if(sql!=null){
            sb.append(sql);
            sb.append(",c.DESCRIPCION");
        }
        Query q=em.createNativeQuery(sb.toString(), C.class);
        List<C> cs=q.getResultList();
        return cs;
    }
    
    public List<C> getCDePedido(PedidoInterfaz p){
        Query q=null;
        if(p.getClass().getName().equals("entities.Pedido")){
            q=em.createNamedQuery("C.findByPedido",C.class);
        }
        else if(p.getClass().getName().equals("entities.PedidoFabrica")){
            q=em.createNamedQuery("C.findByPedidoFabrica",C.class);
        }
        q.setParameter(1, p.getId());
        List<C> cs=q.getResultList();
        return cs;
    }
    
}
